package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Admin;
import com.zaofan.bean.Carrier;
import com.zaofan.bean.CarrierArea;
import com.zaofan.exception.ServiceException;
import com.zaofan.service.CarrierAreaService;
import com.zaofan.service.CarrierService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

/**
 * 运营商控制器
 *
 */
@WebServlet("/carrier")
public class CarrierController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private CarrierService carrierService = new CarrierService();
	private CarrierAreaService carrierAreaService = new CarrierAreaService();

	/**
	 * 运营商登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Carrier carrier = WebUtils.write2bean(request, Carrier.class);
			Carrier target = carrierService.login(carrier);
			request.getSession().setAttribute("carrier", target);
			JsonUtils.writeSuccessJson("查询成功", target, response);
		} catch (ServiceException se) {
			se.printStackTrace();
			JsonUtils.writeBeanFailJson(se.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参数错误!!", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!", response);
	}

	/**
	 * 运营商修改密码
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void modifyPass(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			String oldPass = request.getParameter("oldPass");
			String newPass = request.getParameter("newPass");
			carrierService.updatePass(id, oldPass, newPass);
			JsonUtils.writeSuccessJson("操做成功", "", response);
			return;
		} catch (ServiceException se) {
			se.printStackTrace();
			JsonUtils.writeBeanFailJson(se.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("操做失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("操做失败!!", response);
	}

	/**
	 * 运营商退出
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void loginOut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().removeAttribute("carrier");
		response.sendRedirect(request.getContextPath() + "/loginCarrier.jsp");
	}

	/**
	 * 运营商列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			if (admin.getCityId() != 0) {
				params.put("cityId", admin.getCityId());
			}
			List<Carrier> carriers = carrierService.findAll(params);
			Long count = carrierService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", carriers, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 运营商 区域列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void areas_list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			List<CarrierArea> ares = carrierAreaService.findByCarrierId(id);
			JsonUtils.writeSuccessPageJson("查询成功", ares, 0L, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * 修改运营商 分配区域
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void areas(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Integer id = Integer.valueOf(request.getParameter("id"));
			String areaIds = request.getParameter("areaIds");
			carrierService.updateCarrierAreaInfo(id, areaIds);
			JsonUtils.writeSuccessJson("操作成功", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("操作失败!!", response);
	}

	/**
	 * 增加运营商
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Carrier carrier = WebUtils.write2bean(request, Carrier.class);
			carrierService.add(carrier);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	/**
	 * 更新运营商
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Carrier carrier = WebUtils.write2bean(request, Carrier.class);
			carrierService.updateCarrierInfo(carrier);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	/**
	 * 获取运营商信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			Carrier carrier = carrierService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", carrier, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	/**
	 * 修改运营商状态
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void state(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Carrier carrier = carrierService.findById(id);
			carrier.setState(carrier.getState() == 0 ? 1 : 0);
			carrierService.setCarrierState(carrier);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

}
